/*
 * IMatView.java
 */

package imat;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jdesktop.application.ResourceMap;

/**
 * The application's main frame.
 */
public class IMatView extends FrameView {

    private DataHandler dataHandler;
    Color gron = new Color(0x169721);


    public IMatView(SingleFrameApplication app) {
        super(app);

        initComponents();
        ResourceMap resourceMap = IMatApp.getApplication().getContext().
        getResourceMap(IMatView.class);

        ImageIcon frameIcon = resourceMap.getImageIcon("frame.icon");
        this.getFrame().setIconImage(frameIcon.getImage());

        CardLayout cl = (CardLayout)(viewPanel.getLayout());
        cl.show(viewPanel, "homeCard");

        loadSubMenuHome(homeButton.getText());

        dataHandler = DataHandler.getInstance();
        dataHandler.setMainWindow(this);
        DataHandler.getInstance().getShoppingCart().fireShoppingCartChanged();
        buttonPanel.setVisible(false);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = IMatApp.getApplication().getMainFrame();
            aboutBox = new IMatAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        IMatApp.getApplication().show(aboutBox);
    }

    public JPanel getViewPanel() {
        return viewPanel;
    }

    public JPanel getSideTrolley() {
        return sideTrolley;
    }

    public FavoritesView getFavoritesView() {
        return favoritesView;
    }

    public CheckoutView getCheckoutView() {
        return checkoutView;
    }

    public JButton getFavoriteMenuButton() {
        return favoritesButton;
    }

    public JButton getSavedTrolleysButton() {
        return savedTrolleysButton;
    }

    public ProductListView getProductList() {
        return productList;
    }

    

    public void setSubMenuTitle(String title) {
        subMenuTitle.setText(title);
    }

    public void loadSubMenu(String category) {
        loadSubMenu(category, "");
    }

    public void loadSubMenu(String category, String mark) {
        List<Category> categories = dataHandler.getCategoriesFromTop(category);

        // Set center panel to first category
        if (!categories.isEmpty()) {
            productList.setCategory(categories.get(0).getHandler());
        }
        else {
            productList.clearPanel();
        }

        subMenuPanel.removeAll();

        
        SubMenuItem topItem = null;
        for (Category c : categories) {
            SubMenuItem button = new SubMenuItem(c.getLabel());
            button.setPreferredSize(new Dimension(200, 35));
            button.setActionCommand(c.getHandler().toString());
            subMenuPanel.add(button);
            System.out.println(mark);
            System.out.println(c.getLabel());
            if (c.getLabel().equals(mark)){
                topItem = button;
                productList.setCategory(c.getHandler());
            }
            if (topItem == null) {
                topItem = button;
            }
        }
        topItem.setMarked();
        subMenuPanel.validate();
        subMenuPanel.repaint();
    }

    public void leftPanelButtonPressed(String action) {
        if (action.equals(homeButton.getText())) {
            homeButtonPressed(null);
        }
        else if (action.equals(savedTrolleysButton.getText())) {
            savedTrolleysButtonPressed(null);
        }
        else if (action.equals(infoButton.getText())) {
            infoButtonPressed(null);
        }
        else if (action.equals(favoritesButton.getText())) {
            favoritesButtonPressed(null);
        }
        else if (action.equals(historyButton.getText())) {
            historyButtonPressed(null);
        }
    }

    public void loadSubMenuHome(String clicked) {
        //if (!subMenuTitle.getText().equals("Hem")) {
            subMenuTitle.setText("Hem");
            subMenuPanel.removeAll();

            SubMenuItem button = new SubMenuItem(homeButton.getText());
            button.setPreferredSize(new Dimension(200, 35));
            button.setType(2);
            if (clicked.equals(homeButton.getText())) button.setMarked();
            subMenuPanel.add(button);

            button = new SubMenuItem(savedTrolleysButton.getText());
            button.setPreferredSize(new Dimension(200, 35));
            button.setType(2);
            if (clicked.equals(savedTrolleysButton.getText())) button.setMarked();
            subMenuPanel.add(button);

            button = new SubMenuItem(infoButton.getText());
            button.setPreferredSize(new Dimension(200, 35));
            button.setType(2);
            if (clicked.equals(infoButton.getText())) button.setMarked();
            subMenuPanel.add(button);

            button = new SubMenuItem(favoritesButton.getText());
            button.setPreferredSize(new Dimension(200, 35));
            button.setType(2);
            if (clicked.equals(favoritesButton.getText())) button.setMarked();
            subMenuPanel.add(button);

            button = new SubMenuItem(historyButton.getText());
            button.setPreferredSize(new Dimension(200, 35));
            button.setType(2);
            if (clicked.equals(historyButton.getText())) button.setMarked();
            subMenuPanel.add(button);

            subMenuPanel.validate();
            subMenuPanel.repaint();
        System.out.println(clicked);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        centerPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        backButton = new javax.swing.JButton();
        payButton = new javax.swing.JButton();
        menuPanel = new javax.swing.JPanel();
        menuOfferings = new imat.ImageButton();
        menuDry = new imat.ImageButton();
        menuCold = new imat.ImageButton();
        menuMeatFish = new imat.ImageButton();
        menuFruitVeg = new imat.ImageButton();
        viewPanel = new javax.swing.JPanel();
        productList = new imat.ProductListView();
        favoritesView = new imat.FavoritesView();
        historyView = new imat.HistoryView();
        savedTrolleysView = new imat.SavedTrolleysView();
        homeView = new imat.HomeView();
        searchView = new imat.SearchView();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        kassPanel = new javax.swing.JPanel();
        checkoutView = new imat.CheckoutView();
        payView = new imat.InfoView();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        infoView = new imat.InfoView();
        leftPanel = new javax.swing.JPanel();
        imagePanel2 = new imat.ImagePanel();
        subMenuTitle = new javax.swing.JLabel();
        subMenuPanel = new javax.swing.JPanel();
        topPanel = new javax.swing.JPanel();
        imagePanel1 = new imat.ImagePanel();
        jPanel2 = new javax.swing.JPanel();
        savedTrolleysButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        searchField = new javax.swing.JTextField();
        infoButton = new javax.swing.JButton();
        favoritesButton = new javax.swing.JButton();
        historyButton = new javax.swing.JButton();
        homeButton = new javax.swing.JButton();
        sideTrolley = new imat.SideTrolleyView();

        mainPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        mainPanel.setMinimumSize(new java.awt.Dimension(500, 0));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new java.awt.BorderLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(imat.IMatApp.class).getContext().getResourceMap(IMatView.class);
        centerPanel.setBackground(resourceMap.getColor("centerPanel.background")); // NOI18N
        centerPanel.setName("centerPanel"); // NOI18N
        centerPanel.setPreferredSize(new java.awt.Dimension(525, 300));
        centerPanel.setLayout(new java.awt.BorderLayout());

        buttonPanel.setName("buttonPanel"); // NOI18N

        backButton.setText(resourceMap.getString("backButton.text")); // NOI18N
        backButton.setToolTipText(resourceMap.getString("backButton.toolTipText")); // NOI18N
        backButton.setName("backButton"); // NOI18N
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonPressed(evt);
            }
        });

        payButton.setText(resourceMap.getString("payButton.text")); // NOI18N
        payButton.setToolTipText(resourceMap.getString("payButton.toolTipText")); // NOI18N
        payButton.setName("payButton"); // NOI18N
        payButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                payButtonPressed(evt);
            }
        });

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addGap(117, 117, 117)
                .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(payButton, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(133, Short.MAX_VALUE))
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(payButton, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                    .addComponent(backButton, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))
                .addContainerGap())
        );

        centerPanel.add(buttonPanel, java.awt.BorderLayout.SOUTH);

        menuPanel.setBackground(resourceMap.getColor("menuPanel.background")); // NOI18N
        menuPanel.setName("menuPanel"); // NOI18N
        menuPanel.setPreferredSize(new java.awt.Dimension(450, 30));
        menuPanel.setLayout(new java.awt.GridLayout(1, 5));

        menuOfferings.setActionCommand(resourceMap.getString("menuOfferings.actionCommand")); // NOI18N
        menuOfferings.setImageBackground(resourceMap.getString("menuOfferings.imageBackground")); // NOI18N
        menuOfferings.setImageBackgroundDown(resourceMap.getString("menuOfferings.imageBackgroundDown")); // NOI18N
        menuOfferings.setImageLeft(resourceMap.getString("menuOfferings.imageLeft")); // NOI18N
        menuOfferings.setImageRight(resourceMap.getString("menuOfferings.imageRight")); // NOI18N
        menuOfferings.setName("menuOfferings"); // NOI18N
        menuOfferings.setText(resourceMap.getString("menuOfferings.text")); // NOI18N
        menuOfferings.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                menuActionPerformed(evt);
            }
        });
        menuPanel.add(menuOfferings);

        menuDry.setActionCommand(resourceMap.getString("menuDry.actionCommand")); // NOI18N
        menuDry.setImageBackground(resourceMap.getString("menuDry.imageBackground")); // NOI18N
        menuDry.setImageBackgroundDown(resourceMap.getString("menuDry.imageBackgroundDown")); // NOI18N
        menuDry.setImageLeft(resourceMap.getString("menuDry.imageLeft")); // NOI18N
        menuDry.setImageRight(resourceMap.getString("menuDry.imageRight")); // NOI18N
        menuDry.setName("menuDry"); // NOI18N
        menuDry.setText(resourceMap.getString("menuDry.text")); // NOI18N
        menuDry.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                menuActionPerformed(evt);
            }
        });
        menuPanel.add(menuDry);

        menuCold.setActionCommand(resourceMap.getString("menuCold.actionCommand")); // NOI18N
        menuCold.setImageBackground(resourceMap.getString("menuCold.imageBackground")); // NOI18N
        menuCold.setImageBackgroundDown(resourceMap.getString("menuCold.imageBackgroundDown")); // NOI18N
        menuCold.setImageLeft(resourceMap.getString("menuCold.imageLeft")); // NOI18N
        menuCold.setImageRight(resourceMap.getString("menuCold.imageRight")); // NOI18N
        menuCold.setName("menuCold"); // NOI18N
        menuCold.setText(resourceMap.getString("menuCold.text")); // NOI18N
        menuCold.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                menuActionPerformed(evt);
            }
        });
        menuPanel.add(menuCold);

        menuMeatFish.setActionCommand(resourceMap.getString("menuMeatFish.actionCommand")); // NOI18N
        menuMeatFish.setImageBackground(resourceMap.getString("menuMeatFish.imageBackground")); // NOI18N
        menuMeatFish.setImageBackgroundDown(resourceMap.getString("menuMeatFish.imageBackgroundDown")); // NOI18N
        menuMeatFish.setImageLeft(resourceMap.getString("menuMeatFish.imageLeft")); // NOI18N
        menuMeatFish.setImageRight(resourceMap.getString("menuMeatFish.imageRight")); // NOI18N
        menuMeatFish.setName("menuMeatFish"); // NOI18N
        menuMeatFish.setText(resourceMap.getString("menuMeatFish.text")); // NOI18N
        menuMeatFish.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                menuActionPerformed(evt);
            }
        });
        menuPanel.add(menuMeatFish);

        menuFruitVeg.setActionCommand(resourceMap.getString("menuFruitVeg.actionCommand")); // NOI18N
        menuFruitVeg.setImageBackground(resourceMap.getString("menuFruitVeg.imageBackground")); // NOI18N
        menuFruitVeg.setImageBackgroundDown(resourceMap.getString("menuFruitVeg.imageBackgroundDown")); // NOI18N
        menuFruitVeg.setImageLeft(resourceMap.getString("menuFruitVeg.imageLeft")); // NOI18N
        menuFruitVeg.setImageRight(resourceMap.getString("menuFruitVeg.imageRight")); // NOI18N
        menuFruitVeg.setName("menuFruitVeg"); // NOI18N
        menuFruitVeg.setText(resourceMap.getString("menuFruitVeg.text")); // NOI18N
        menuFruitVeg.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                menuActionPerformed(evt);
            }
        });
        menuPanel.add(menuFruitVeg);

        centerPanel.add(menuPanel, java.awt.BorderLayout.NORTH);

        viewPanel.setBackground(resourceMap.getColor("viewPanel.background")); // NOI18N
        viewPanel.setName("viewPanel"); // NOI18N
        viewPanel.setPreferredSize(new java.awt.Dimension(540, 511));
        viewPanel.setLayout(new java.awt.CardLayout());

        productList.setBackground(resourceMap.getColor("productList.background")); // NOI18N
        productList.setName("productList"); // NOI18N
        viewPanel.add(productList, "productsCard");

        favoritesView.setName("favoritesView"); // NOI18N
        viewPanel.add(favoritesView, "favoritesCard");

        historyView.setName("historyView"); // NOI18N
        viewPanel.add(historyView, "historyCard");

        savedTrolleysView.setName("savedTrolleysView"); // NOI18N
        viewPanel.add(savedTrolleysView, "savedTrolleysCard");

        homeView.setName("homeView"); // NOI18N
        homeView.setPreferredSize(new java.awt.Dimension(499, 331));
        viewPanel.add(homeView, "homeCard");

        searchView.setName("searchView"); // NOI18N
        searchView.setPreferredSize(new java.awt.Dimension(540, 200));
        viewPanel.add(searchView, "searchCard");

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(520, 642));

        kassPanel.setName("kassPanel"); // NOI18N
        kassPanel.setLayout(new java.awt.CardLayout());

        checkoutView.setBackground(resourceMap.getColor("checkoutView.background")); // NOI18N
        checkoutView.setName("checkoutView"); // NOI18N
        kassPanel.add(checkoutView, "checkoutCard");

        payView.setBackground(resourceMap.getColor("payView.background")); // NOI18N
        payView.setName("payView"); // NOI18N
        kassPanel.add(payView, "payCard");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(kassPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(kassPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(109, Short.MAX_VALUE))
        );

        jScrollPane2.setViewportView(jPanel3);

        viewPanel.add(jScrollPane2, "checkoutCard");
        jScrollPane2.getVerticalScrollBar().setUnitIncrement(100);

        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setLayout(new java.awt.BorderLayout());

        jPanel5.setName("jPanel5"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveInfo(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(198, 198, 198)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(200, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6.add(jPanel5, java.awt.BorderLayout.SOUTH);

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setPreferredSize(new java.awt.Dimension(848, 649));
        jScrollPane1.getVerticalScrollBar().setUnitIncrement(100);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(500, 647));

        infoView.setMinimumSize(new java.awt.Dimension(342, 535));
        infoView.setName("infoView"); // NOI18N
        infoView.setPreferredSize(new java.awt.Dimension(339, 535));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(infoView, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(infoView, javax.swing.GroupLayout.PREFERRED_SIZE, 560, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(jPanel1);

        jPanel6.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        viewPanel.add(jPanel6, "infoCard");

        centerPanel.add(viewPanel, java.awt.BorderLayout.CENTER);

        mainPanel.add(centerPanel, java.awt.BorderLayout.CENTER);

        leftPanel.setBackground(resourceMap.getColor("leftPanel.background")); // NOI18N
        leftPanel.setMinimumSize(new java.awt.Dimension(150, 0));
        leftPanel.setName("leftPanel"); // NOI18N
        leftPanel.setPreferredSize(new java.awt.Dimension(150, 495));

        imagePanel2.setBackground(resourceMap.getColor("imagePanel2.background")); // NOI18N
        imagePanel2.setImage(resourceMap.getString("imagePanel2.image")); // NOI18N
        imagePanel2.setName("imagePanel2"); // NOI18N

        subMenuTitle.setBackground(resourceMap.getColor("subMenuTitle.background")); // NOI18N
        subMenuTitle.setFont(resourceMap.getFont("subMenuTitle.font")); // NOI18N
        subMenuTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        subMenuTitle.setText(resourceMap.getString("subMenuTitle.text")); // NOI18N
        subMenuTitle.setBorder(javax.swing.BorderFactory.createEmptyBorder(7, 5, 5, 5));
        subMenuTitle.setName("subMenuTitle"); // NOI18N

        subMenuPanel.setBackground(resourceMap.getColor("subMenuPanel.background")); // NOI18N
        subMenuPanel.setName("subMenuPanel"); // NOI18N
        subMenuPanel.setOpaque(false);
        subMenuPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        javax.swing.GroupLayout imagePanel2Layout = new javax.swing.GroupLayout(imagePanel2);
        imagePanel2.setLayout(imagePanel2Layout);
        imagePanel2Layout.setHorizontalGroup(
            imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imagePanel2Layout.createSequentialGroup()
                .addGroup(imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(subMenuPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(imagePanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(subMenuTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        imagePanel2Layout.setVerticalGroup(
            imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imagePanel2Layout.createSequentialGroup()
                .addComponent(subMenuTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(subMenuPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 491, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(64, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addComponent(imagePanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addComponent(imagePanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(109, Short.MAX_VALUE))
        );

        mainPanel.add(leftPanel, java.awt.BorderLayout.WEST);

        topPanel.setBackground(resourceMap.getColor("topPanel.background")); // NOI18N
        topPanel.setName("topPanel"); // NOI18N
        topPanel.setPreferredSize(new java.awt.Dimension(789, 90));

        imagePanel1.setImage(resourceMap.getString("imagePanel1.image")); // NOI18N
        imagePanel1.setMinimumSize(new java.awt.Dimension(500, 90));
        imagePanel1.setName("imagePanel1"); // NOI18N
        imagePanel1.setOpaque(false);
        imagePanel1.setPreferredSize(new java.awt.Dimension(500, 173));

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setOpaque(false);

        savedTrolleysButton.setText(resourceMap.getString("savedTrolleysButton.text")); // NOI18N
        savedTrolleysButton.setToolTipText(resourceMap.getString("savedTrolleysButton.toolTipText")); // NOI18N
        savedTrolleysButton.setName("savedTrolleysButton"); // NOI18N
        savedTrolleysButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savedTrolleysButtonPressed(evt);
            }
        });

        searchButton.setText(resourceMap.getString("searchButton.text")); // NOI18N
        searchButton.setToolTipText(resourceMap.getString("searchButton.toolTipText")); // NOI18N
        searchButton.setName("searchButton"); // NOI18N
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchPerformed(evt);
            }
        });

        searchField.setText(resourceMap.getString("searchField.text")); // NOI18N
        searchField.setToolTipText(resourceMap.getString("searchField.toolTipText")); // NOI18N
        searchField.setName("searchField"); // NOI18N
        searchField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchPerformed(evt);
            }
        });
        searchField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                searchFieldFocus(evt);
            }
        });

        infoButton.setText(resourceMap.getString("infoButton.text")); // NOI18N
        infoButton.setToolTipText(resourceMap.getString("infoButton.toolTipText")); // NOI18N
        infoButton.setName("infoButton"); // NOI18N
        infoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                infoButtonPressed(evt);
            }
        });

        favoritesButton.setText(resourceMap.getString("favoritesButton.text")); // NOI18N
        favoritesButton.setToolTipText(resourceMap.getString("favoritesButton.toolTipText")); // NOI18N
        favoritesButton.setName("favoritesButton"); // NOI18N
        favoritesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                favoritesButtonPressed(evt);
            }
        });

        historyButton.setText(resourceMap.getString("historyButton.text")); // NOI18N
        historyButton.setToolTipText(resourceMap.getString("historyButton.toolTipText")); // NOI18N
        historyButton.setName("historyButton"); // NOI18N
        historyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                historyButtonPressed(evt);
            }
        });

        homeButton.setText(resourceMap.getString("homeButton.text")); // NOI18N
        homeButton.setToolTipText(resourceMap.getString("homeButton.toolTipText")); // NOI18N
        homeButton.setName("homeButton"); // NOI18N
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(27, Short.MAX_VALUE)
                .addComponent(homeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(savedTrolleysButton)
                .addGap(6, 6, 6)
                .addComponent(infoButton)
                .addGap(5, 5, 5)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(favoritesButton)
                        .addGap(5, 5, 5)
                        .addComponent(historyButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(searchField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchButton))))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {favoritesButton, historyButton, infoButton, savedTrolleysButton});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(infoButton)
                        .addComponent(savedTrolleysButton)
                        .addComponent(homeButton))
                    .addComponent(favoritesButton)
                    .addComponent(historyButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, imagePanel1Layout.createSequentialGroup()
                .addContainerGap(271, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imagePanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 887, Short.MAX_VALUE)
        );
        topPanelLayout.setVerticalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
        );

        mainPanel.add(topPanel, java.awt.BorderLayout.NORTH);

        sideTrolley.setBackground(resourceMap.getColor("sideTrolley.background")); // NOI18N
        sideTrolley.setImage(resourceMap.getString("sideTrolley.image")); // NOI18N
        sideTrolley.setName("sideTrolley"); // NOI18N
        sideTrolley.setPreferredSize(new java.awt.Dimension(200, 900));
        mainPanel.add(sideTrolley, java.awt.BorderLayout.EAST);

        setComponent(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void infoButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_infoButtonPressed
        loadSubMenuHome(infoButton.getText());
        CardLayout cl = (CardLayout)(viewPanel.getLayout());
        cl.show(viewPanel, "infoCard");
        exitBuyView();
        infoView.updateInfo();
}//GEN-LAST:event_infoButtonPressed

    private void favoritesButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_favoritesButtonPressed
        loadSubMenuHome(favoritesButton.getText());
        CardLayout cl = (CardLayout)(viewPanel.getLayout());
        cl.show(viewPanel, "favoritesCard");
        favoritesView.updateFavorites();
        exitBuyView();
}//GEN-LAST:event_favoritesButtonPressed

    private void savedTrolleysButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savedTrolleysButtonPressed
        loadSubMenuHome(savedTrolleysButton.getText());
        CardLayout cl = (CardLayout)(viewPanel.getLayout());
        cl.show(viewPanel, "savedTrolleysCard");

        savedTrolleysView.fillPanel();
        exitBuyView();
}//GEN-LAST:event_savedTrolleysButtonPressed

    private void historyButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_historyButtonPressed
        loadSubMenuHome(historyButton.getText());
        CardLayout cl = (CardLayout)(viewPanel.getLayout());
        cl.show(viewPanel, "historyCard");
        exitBuyView();

        historyView.fillPanel();

}//GEN-LAST:event_historyButtonPressed

    private void menuActionPerformed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuActionPerformed
        ImageButton source = (ImageButton)evt.getSource();
        if (source.isButtonClicked(evt)) {
            CardLayout cl = (CardLayout) (viewPanel.getLayout());
            cl.show(viewPanel, "productsCard");

            loadSubMenu(source.getActionCommand());
            
            setSubMenuTitle(source.getText());
            DataHandler.getInstance().category = source.getActionCommand();
            DataHandler.getInstance().categoryLabel = source.getText();
            
            exitBuyView();
          
        }
    }//GEN-LAST:event_menuActionPerformed

    private void homeButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonPressed
        loadSubMenuHome(homeButton.getText());
        CardLayout cl = (CardLayout)(viewPanel.getLayout());
        cl.show(viewPanel, "homeCard");
        exitBuyView();
        DataHandler.getInstance().getMainWindow().getHomeView().getjLabel1().setIcon(new ImageIcon("images/pierre_forsta1.png"));
    }//GEN-LAST:event_homeButtonPressed

    private void searchPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchPerformed
        CardLayout cl = (CardLayout)(viewPanel.getLayout());
        cl.show(viewPanel, "searchCard");

        subMenuTitle.setText("");
        subMenuPanel.removeAll();
        subMenuPanel.validate();
        subMenuPanel.repaint();

        searchView.search(searchField.getText());
        exitBuyView();
    }//GEN-LAST:event_searchPerformed

    private void saveInfo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveInfo
        infoView.saveInfo();
    }//GEN-LAST:event_saveInfo

    private void payButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_payButtonPressed
        if (payButton.getText().equals("Betala ->")) {
            CardLayout cl = (CardLayout)(kassPanel.getLayout());
            cl.show(kassPanel, "payCard");
            payButton.setText("Godkänn köp");
            backButton.setText("Tillbaka");
            payButton.setToolTipText("Genomför köpet");
            backButton.setToolTipText("Tillbaka till kvittot");
            payView.updateInfo();
        } else if (payButton.getText().equals("Godkänn köp")) {
             payView.saveInfo();
             if (DataHandler.getInstance().isCustomerComplete() && payView.isPayInfoComplete()) {
                CardLayout cl = (CardLayout)(kassPanel.getLayout());
                cl.show(kassPanel, "checkoutCard");
                payButton.setText("Betala ->");
                backButton.setText("<- Fortsätt handla");
                payButton.setToolTipText("Fortsätt med ditt köp");
                backButton.setToolTipText("Gå tillbaka till affären");
                loadSubMenuHome(homeButton.getText());
                cl = (CardLayout)(viewPanel.getLayout());
                cl.show(viewPanel, "homeCard");
                exitBuyView();
                DataHandler.getInstance().getMainWindow().getHomeView().getjLabel1().setIcon(new ImageIcon("images/pierre_andra.png"));
                DataHandler.getInstance().placeOrder();
             }
        }

    }//GEN-LAST:event_payButtonPressed

    private void backButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonPressed
        
        if (backButton.getText().equals("<- Fortsätt handla")) {
            CardLayout cl = (CardLayout)(viewPanel.getLayout());
            if (DataHandler.getInstance().category == null) {
                //setSubMenuTitle(DataHandler.getInstance().categoryLabel);
                loadSubMenuHome(homeButton.getText());
                cl.show(viewPanel, "homeCard");
            }
            else {
                setSubMenuTitle(DataHandler.getInstance().categoryLabel);
                loadSubMenu(DataHandler.getInstance().category, DataHandler.getInstance().mark);
                cl.show(viewPanel, "productsCard");
            }
            
            exitBuyView();
        } else if (backButton.getText().equals("Tillbaka")) {
            CardLayout cl = (CardLayout)(kassPanel.getLayout());
            cl.show(kassPanel, "checkoutCard");
            payButton.setText("Betala ->");
            backButton.setText("<- Fortsätt handla");
            DataHandler.getInstance().showSideTrolley();
        }
    }//GEN-LAST:event_backButtonPressed

    private void searchFieldFocus(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchFieldFocus
        searchField.selectAll();
    }//GEN-LAST:event_searchFieldFocus

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    public javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel centerPanel;
    private imat.CheckoutView checkoutView;
    private javax.swing.JButton favoritesButton;
    private imat.FavoritesView favoritesView;
    private javax.swing.JButton historyButton;
    private imat.HistoryView historyView;
    private javax.swing.JButton homeButton;
    private imat.HomeView homeView;
    private imat.ImagePanel imagePanel1;
    private imat.ImagePanel imagePanel2;
    private javax.swing.JButton infoButton;
    private imat.InfoView infoView;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel kassPanel;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JPanel mainPanel;
    private imat.ImageButton menuCold;
    private imat.ImageButton menuDry;
    private imat.ImageButton menuFruitVeg;
    private imat.ImageButton menuMeatFish;
    private imat.ImageButton menuOfferings;
    private javax.swing.JPanel menuPanel;
    private javax.swing.JButton payButton;
    public imat.InfoView payView;
    private imat.ProductListView productList;
    private javax.swing.JButton savedTrolleysButton;
    private imat.SavedTrolleysView savedTrolleysView;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchField;
    private imat.SearchView searchView;
    public imat.SideTrolleyView sideTrolley;
    public javax.swing.JPanel subMenuPanel;
    public javax.swing.JLabel subMenuTitle;
    private javax.swing.JPanel topPanel;
    private javax.swing.JPanel viewPanel;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;

    public HomeView getHomeView() {
        return homeView;
    }
    
    public void exitBuyView() {
        DataHandler.getInstance().showSideTrolley();
        buttonPanel.setVisible(false);
        CardLayout cl = (CardLayout)(kassPanel.getLayout());
        cl.show(kassPanel, "checkoutCard");
        payButton.setText("Betala ->");
        backButton.setText("<- Fortsätt handla");
    }
}
